#include "init.h"

const char *gWinTileStr = "Test SDL";
SDL_Window *gWin = NULL;
SDL_Surface *gWinSurface = NULL;
SDL_Rect gWinRect;
SDL_Renderer *gRender;

bool init(void){
    bool success = true;
    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0) {
        SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
        success = false;
    }else{
        gWin = SDL_CreateWindow(gWinTileStr,
                               SDL_WINDOWPOS_UNDEFINED,
                               SDL_WINDOWPOS_UNDEFINED,
                               WINDOW_WIDTH,
                               WINDOW_HEIGHT,
                               SDL_WINDOW_OPENGL);
        if(!gWin){
            printf("Could not create window: %s\n", SDL_GetError());
            success = false;
        }else{
            gWinRect.x = 0;
            gWinRect.y = 0;
            gWinRect.w = WINDOW_WIDTH;
            gWinRect.h = WINDOW_HEIGHT;
            gWinSurface = SDL_GetWindowSurface(gWin);
        }
    }

    return success;
}

bool init2(void){
    bool success = true;
    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0) {
        SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
        success = false;
    }else{
        gWin = SDL_CreateWindow(gWinTileStr,
                               SDL_WINDOWPOS_UNDEFINED,
                               SDL_WINDOWPOS_UNDEFINED,
                               WINDOW_WIDTH,
                               WINDOW_HEIGHT,
                               SDL_WINDOW_OPENGL);
        if(!gWin){
            printf("Could not create window: %s\n", SDL_GetError());
            success = false;
        }else{
            gWinRect.x = 0;
            gWinRect.y = 0;
            gWinRect.w = WINDOW_WIDTH;
            gWinRect.h = WINDOW_HEIGHT;
            gRender = SDL_CreateRenderer(gWin, -1, SDL_RENDERER_ACCELERATED);
            if(!gRender){
                printf("Failed to create renderer: %s\n", SDL_GetError());
            }else{
                SDL_SetRenderDrawColor(gRender, 0xff, 0xff, 0xff, 0xff);
            }
        }
    }

    return success;
}

bool initWindowAndRenderer(SDL_Window **win, SDL_Renderer **renderer){
    bool success = true;
    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0) {
        SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
        success = false;
    }else{
        *win = SDL_CreateWindow(gWinTileStr,
                               SDL_WINDOWPOS_UNDEFINED,
                               SDL_WINDOWPOS_UNDEFINED,
                               WINDOW_WIDTH,
                               WINDOW_HEIGHT,
                               SDL_WINDOW_OPENGL);
        if(!(*win)){
            printf("Could not create window: %s\n", SDL_GetError());
            success = false;
        }else{
            gWinRect.x = 0;
            gWinRect.y = 0;
            gWinRect.w = WINDOW_WIDTH;
            gWinRect.h = WINDOW_HEIGHT;
            *renderer = SDL_CreateRenderer(*win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
            if(!renderer){
                printf("Failed to create renderer: %s\n", SDL_GetError());
                success = false;
            }else{
                SDL_SetRenderDrawColor(*renderer, 0xff, 0xff, 0xff, 0xff);
                SDL_RenderClear(*renderer);

                int flags = IMG_INIT_JPG|IMG_INIT_PNG;
                int initted = IMG_Init(flags);
                if((initted & flags) != flags) {
                    printf("IMG_Init failed: %s\n", IMG_GetError());
                }
                if(TTF_Init() == -1) {
                    printf("TTF_Init failed: %s\n", TTF_GetError());
                }
            }
        }
    }

    return success;
}

